package com.qzsf.backend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qzsf.backend.common.Result;
import com.qzsf.backend.dto.NotificationDTO;
import com.qzsf.backend.entity.Notification;
import com.qzsf.backend.mapper.NotificationMapper;
import com.qzsf.backend.mapper.RegistrationMapper;
import com.qzsf.backend.service.NotificationService;
import com.qzsf.backend.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/11/22 21:35 周五
 * @Email 1429278462@qq.com
 * @Remark 通知服务层实现类
 * @Version 1.0
 */
@Service
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;
    @Autowired
    private RegistrationMapper registrationMapper;
    @Override
    public Result sendToUser(NotificationDTO notificationDTO) {
        Notification notification = new Notification();
        notification.setAuthId(notificationDTO.getAuthId());
        notification.setTitle(notificationDTO.getTitle());
        notification.setMessage(notificationDTO.getMessage());
        notification.setIsRead(0);
        if(notificationMapper.createNotification(notification)>0){
            return Result.success();
        }else {
            return Result.error("通知创建失败");
        }
    }

    @Override
    public Result sendToEventUsers(NotificationDTO notificationDTO) {
        List<Integer> userIds = registrationMapper.findUserIdsByEventId(notificationDTO.getEventId());
        return batchCreateNotifications(userIds, notificationDTO);
    }

    @Override
    public Result sendToUsers(List<Integer> ids, NotificationDTO notificationDTO) {
        return batchCreateNotifications(ids, notificationDTO);
    }

    @Override
    public Result getNotification(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        return Result.success(new PageInfo<>(notificationMapper.getNotification()));
    }

    @Override
    public Result getMessageCount() {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        return Result.success(notificationMapper.getUnreadMessageCount(id));
    }

    @Override
    public Result getUserMessage() {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        return Result.success(notificationMapper.getUserNotification(id));
    }

    @Override
    public Result readUserMessage() {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        return Result.success(notificationMapper.readUserMessage(id));
    }

    private Result batchCreateNotifications(List<Integer> ids, NotificationDTO notificationDTO) {
        for (Integer userId : ids) {
            Notification notification = new Notification();
            notification.setAuthId(userId);
            notification.setTitle(notificationDTO.getTitle());
            notification.setMessage(notificationDTO.getMessage());
            notification.setIsRead(0);
            if(notificationMapper.createNotification(notification)<=0){
                return Result.error("通知创建失败"+"失败用户ID"+userId);
            }
        }
        return Result.success();
    }
}
